// locutor
// (c) 2008-2009, Normen Müller, normen.mueller@gmail.com  
// $Id$
package locutor.model.parsing.grgen

import collection.mutable.HashSet

/** 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.2 */
private [grgen] object XGRSParser {
  
  val reserved = new HashSet[String] ++ List(
    "def", "true", "false"
  )
  
  val delimiters = new HashSet[String] ++ List(
    "!", "*", "+", "[", "]", ":", "$", "&&", "&", "||", "|", "<", ">", "(", ")", ",", "=", "%", "?", "^"
  )
  
}

/** 
 * Restrictions:
 * - parallel call rule
 * 
 * @todo adjust with SequenceParser.csc
 * 
 * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 * @version 0.0.2 */
private [grgen] trait XGRSParser extends BaseParser {
  
  def rewrite_seq: Parser[Any] =
    opt("!") ~ rewrite_term ~ opt("*" | "+" | "[" ~ ((numeric | "*" | "+") ~ opt(":" ~ (numeric | "*"))) ~ "]") ~
    (( opt("$") ~ "&&" ~ rewrite_seq
     | opt("$") ~ "||" ~ rewrite_seq
     | opt("$") ~ "&" ~ rewrite_seq
     | opt("$") ~ "|" ~ rewrite_seq
    )*)
  
  def rewrite_term =
    ( var_assign
    | rule_exec
    | "<" ~ rewrite_seq ~ ">"
    | "(" ~ rewrite_seq ~ ")"
    | ident
    | "def" ~ "(" ~ vars ~ ")"
    | "true"
    | "false"
    )
  
  def vars       = repsep((ident | numeric) ~ opt(":" ~ ident), ",")
  
  def rule       = ident ~ opt("(" ~ vars ~ ")")
  
  def var_assign = ident ~ opt(":" ~ ident) ~ "=" ~ (text | numeric | "(" ~ rewrite_seq ~ ")" | "true" | "false")
  
  def rule_exec  = opt("(" ~ vars ~ ")" ~ "=") ~ (("[" ~ opt("%") ~ opt("?") ~ rule ~ "]") | rule)

}
